<template>
  <div class="warp">
    <div class="title">{{ id ? "编辑" : "新增" }}</div>
    <div class="login-group">
      <div class="login-group-item">
        <div class="login-label">url</div>
        <div>
          <input v-model="item.url" type="text" placeholder="url" />
        </div>
      </div>
      <div class="login-group-item">
        <div class="login-label">名称</div>
        <div class="login-input">
          <input v-model="item.label" type="text" placeholder="名称" />
        </div>
      </div>
      <div class="login-group-btn">
        <div class="btn" @click="save">保存</div>
      </div>
    </div>
  </div>
</template>

<script setup lang="ts">
import { ItemInfo } from "@/api/types";
import { useItemsStore } from "@/store/modules/items";
import { onMounted, ref } from "vue";
import { useRoute, useRouter } from "vue-router";
const route = useRoute();
const _useItemsStore = useItemsStore();
const item = ref<ItemInfo>({
  label: "",
  url: "",
  id: "",
});
const router = useRouter();
const save = () => {
  _useItemsStore
    .saveItemInfo(item.value.id, item.value.url, item.value.label)
    .then((result) => {
      router.push({
        path: "/list",
      });
    })
    .catch((err) => {});
};
const {
  query: { id },
} = route;
const getInfo = () => {
  if (id) {
    item.value = _useItemsStore.getItemInfo(id as string);
  }
};
onMounted(() => {
  getInfo();
});
</script>

<style lang="scss" scoped>
.warp {
  padding-top: calc(30vh - 120px);
  height: 100vh;
  width: 100%;
  .title {
    text-align: center;
    font-size: 24px;
    line-height: 50px;
    font-weight: bold;
  }
  .login-group {
    background: #ffffff;
    border-radius: 5px;
    padding: 45px 30px 0px;
    box-shadow: 0px 6px 10px rgba(144, 147, 153, 0.5);
    margin: auto;
    width: 320px;
    height: 240px;
    position: relative;
    .login-group-item {
      height: 50px;
      font-size: 16px;
      display: flex;
      .login-label {
        width: 80px;
      }
      .login-input {
      }
    }
    .login-group-btn {
      width: calc(100% - 60px);
      position: absolute;
      bottom: 20px;
      text-align: center;
      display: flex;
      justify-content: center;
      .btn {
        line-height: 28px;
        height: 30px;
        width: 90px;
        border-radius: 3px;
        border: 1px #dedfe0 solid;
      }
    }
  }
}
</style>
